<?php

namespace App\Admin\Controllers\Account;

use App\Admin\Models\SiteStore;
use App\Admin\Services\Financial;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Common\Models\StoreRenewalLog;
use App\Common\Services\CommonExcelExport;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Maatwebsite\Excel\Facades\Excel;

class StoreFeeController extends Controller
{
    /**
     * 获取续费列表
     */
    public function getlist(Request $request)
    {
        $list = StoreRenewalLog::getListByAdmin($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 获取续费详情
     */
    public function getinfo(Request $request)
    {
        $id = $request->id ?? 0;
        $info = StoreRenewalLog::where(['id' => $id])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    /**
     * 续费确认
     */
    public function confirmfee(Request $request)
    {
        $id = $request->id ?? 0;
        $info = StoreRenewalLog::where([['id', $id]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '续费记录不存在']);
        }
        if ($info->is_pay != 2) {
            return response()->json(['code' => '001', 'msg' => '续费付款状态错误，请确认']);
        }
        if ($info->is_confirm != 1) {
            return response()->json(['code' => '001', 'msg' => '续费状态错误，请确认']);
        }
        $sn_code='XF'.date('YmdHis').time().mt_rand(1000,9999);
        $fee=Financial::getFeeByMoney($info->soft_year_fee,$info->pay_type);
        StoreRenewalLog::where([['id', $id]])->update(['is_confirm' => 2, 'confirm_at' => time(),'sn_code'=>$sn_code,'fee'=>$fee]);
        //增加时间--按照合同到期时间增加一年还是按照续费订单里面的时间
        $storeinfo = SiteStore::where(['id' => $info->store_id])->first();
        //更改合同签订时间为上次合同到期时间，同时更改合同审核状态为未审核
        if ($info->contract_end_at < time()) {
            $info->contract_end_at = time() + 365*86400;
        }
        SiteStore::where(['id' => $info->store_id])->update(['fee_log_id' => 0, 'is_can_use'=>1, 'contract_audit_status' => 2,  'contract_end_at' => $info->contract_end_at, 'pay_type_name' => $info->pay_type_name ?? '', 'pay_type' => $info->pay_type]);
        //更新到期时间
        Redis::set('store_timeout_at_byid_' . intval($info->store_id), intval($info->contract_end_at));

        Financial::createMoneyLog($info->pay_type, $info->soft_year_fee, $sn_code);

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
    /**
     * 导出
     */
    public function doexport(Request $request)
    { //isgetall=1;
        $list = StoreRenewalLog::getListByAdmin($request)['list'];
        $exceldata = [];
        $row = [
            ['company_name' => '用户门店续费管理'],
            [
                'company_name' => '所属公司',
                'store_name' => '门店名称',
                'contract_end_at' => '合同到期时间',
                'pay_at' => '缴费日期',
                'soft_year_fee' => '缴费金额',
               // 'bank_card_no' => '收款账户',
                'pay_type_name' => '支付方式',
            ]
        ];
        foreach ($list as $val) {
            $exceldata[] = [
                'company_name' => $val['companyinfo']['company_name'],
                'store_name' => $val['storeinfo']['store_name'],
                'contract_end_at' => date('Y-m-d H:i:s', $val['storeinfo']['contract_end_at']),
                'pay_at' => $val['pay_at']>0?date('Y-m-d H:i:s', $val['pay_at']):'',
                'soft_year_fee' => $val['soft_year_fee']/100,
               // 'bank_card_no' => $val['bank_card_no'],
                'pay_type_name' => $val['pay_type_name'],
            ];
        }

        $filename = '用户公司续费信息导出_' . time() . '.xlsx';
        return Excel::download(new CommonExcelExport($row, $exceldata), $filename);
    }
}
